﻿// -----------------------------------------------------------------------
// <copyright file="AuditLogRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AuditLogRepository : AbstractRepository<AuditLog>
    {
        public AuditLogRepository()
        {
            base._contextHelper = null;
            base._context = new Context.AGRContext();
        }

        public override IQueryable<AuditLog> GetAll()
        {
            var result = (from x in base._context.AuditLogs
                          select new AuditLog
                          {
                              Id = x.Id,
                              TableName = x.TableName,
                              ColumnName = x.ColumnName,
                              Action = x.Action,
                              OldValue = x.OldValue,
                              NewValue = x.NewValue,
                              EmployeeCode = x.EmployeeCode,
                              AuditDate = x.AuditDate,
                          });

            return result;
        }

        public override void Add(AuditLog entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.AuditLog entityToAdd = new Context.AuditLog
            {
                TableName = entity.TableName,
                ColumnName = entity.ColumnName,
                Action = entity.Action,
                OldValue = entity.OldValue,
                NewValue = entity.NewValue,
                EmployeeCode = entity.EmployeeCode,
                AuditDate = DateTime.Now,
            };

            base._context.AuditLogs.InsertOnSubmit(entityToAdd);
        }

        public override void Update(AuditLog entity)
        {
            throw new NotImplementedException();

            //if (entity == null)
            //    throw new ArgumentNullException();

            //Context.AuditLog entityToUpdate = base._context.AuditLogs.Single(x => x.ColumnName.Equals(entity.ColumnName));

            //entityToUpdate.TableName = entity.TableName;
            //entityToUpdate.ColumnName = entity.ColumnName;
            //entityToUpdate.Action = entity.Action;
            //entityToUpdate.OldValue = entity.OldValue;
            //entityToUpdate.NewValue = entity.NewValue;
            //entityToUpdate.EmployeeCode = entity.EmployeeCode;
            //entityToUpdate.AuditDate = entity.AuditDate;
        }

        public override void Delete(AuditLog entity)
        {
            throw new NotImplementedException();

            //if (entity == null)
            //    throw new ArgumentNullException();

            //Context.AuditLog entityToDelete = base._context.AuditLogs.Single(x => x.ColumnName.Equals(entity.ColumnName));

            //base._context.AuditLogs.DeleteOnSubmit(entityToDelete);
        }
    }
}
